package com.tencent.cloud.iov.kernel.service.interceptor;


import com.tencent.cloud.iov.util.SystemUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.Version;

/**
 * 请求头 添加统计信息
 *
 * @author vinsonhuang
 * @date 2018/5/30
 */
public class StatisticsInterceptor implements Interceptor {

    private static final String TAG = StatisticsInterceptor.class.getSimpleName();

    private static final String USER_AGENT = "User-Agent";

    private static final String ANDROID = "Android";

    private static final String BUILD = "Build";

    private String mUserAgentInfo;

    public StatisticsInterceptor() {
        mUserAgentInfo = getUserAgentInfo();
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        builder.header(USER_AGENT, mUserAgentInfo);
        return chain.proceed(builder.build());
    }

    private String getUserAgentInfo() {
        StringBuilder userAgent = new StringBuilder(Version.userAgent());
        userAgent.append(" (").append(ANDROID).append(" ").append(SystemUtils.getSystemVersion())
                .append("; ").append(SystemUtils.getDeviceBrand())
                .append(" ").append(SystemUtils.getSystemModel())
                .append(" ").append(BUILD).append("/").append(SystemUtils.getSystemIncremental())
                .append(")");
        return userAgent.toString();
    }
}
